package com.central.apps.service.impl;


import java.util.List;
import java.util.Map;

import com.central.app.dao.CommonDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.central.apps.dao.TestDao;
import com.central.apps.service.TestService;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    public TestDao testDao ;
    @Autowired
    private CommonDao commonDao;

    @Override
    public List<Map<String, Object>> findFirstApp() {
        List<Map<String, Object>> refList = testDao.findBySql("select * from biz_app limit 1");
        return refList;
    }

    @Override
    public String AllParent() throws Exception {
        //修改所有父级
        commonDao.updateBySql("update bl_locationmanagement  set parents = CONCAT(\"'\",location,\"'\")  where parent = '' or parent is null");
        StringBuffer sbsql = new StringBuffer("select t.id,t.LOCATION,t.PARENTS from bl_locationmanagement t  where (parent = '' or parent is null) ");
        //查询一条数据
        List<Map<String, Object>> mapList = commonDao.findBySql(sbsql.toString());
        //进入递归
        in(mapList);
        return "操作成功";
    }
    public void in (List<Map<String, Object>> chlidList ) {
        //判断非空
        if(CollectionUtils.isNotEmpty(chlidList)){
            //循环数据
            for(Map<String, Object> map : chlidList){
                //拿到parent和locaiton
                String id = MapUtils.getString(map,"id");
                String parents = MapUtils.getString(map,"PARENTS");
                String location = MapUtils.getString(map,"LOCATION");
                //更新当前层级的下一层级
                String update = "update bl_locationmanagement set parents = CONCAT(\""+parents+"\",\",'\",location,\"'\") where parent = '"+location+"'";
                commonDao.updateBySql(update);
                //查询当前层级的下一层级
                List<Map<String, Object>> chlidLists = commonDao.findBySql("select t.id,t.LOCATION,t.PARENTS from bl_locationmanagement t where t.parent = '"+location+"' ");
                //再次进入递归
                in(chlidLists);
            }
        }
    }
}
